/**
 * @Description: 订单详情
 * @Author: Administrator
 * @Date: 2017/8/29
 * @Copyright: Created by Panxsoft.
 */


var order_invoice =  new Resource(API_LIST.ORDER_INVOICE, Vue.http);

new Vue({
    el:"#order_invoice",
    data: function () {
        return{
            logo: '',
            order: {},
            pay_status_map: {},
            orderDetails:[],
        }
    },

    computed: {
        getWechatInfo: function () {
            return this.order.wechat_user ? this.order.wechat_user : {};
        },
        getGender: function () {
            if(typeof this.getWechatInfo.sex === 'undefined'){
                return '无';
            }else {
                return ['无','男','女'][this.getWechatInfo.sex];
            }
        },
        getOrderAddress: function () {
            var address = this.order.province + this.order.city + this.order.district + this.order.address;
            return address ? address : '无';
        }
    },

    methods: {
        /**
         * 请求订单详情
         * @param {number} id 订单id
         * @return {undefined}
         */
        fetchOrderDetail: function (id) {
            var self = this;
            order_invoice.get(id).then(function (response) {
                if( response.data.code !== 0 ){
                    throw new Error(response.data.error );
                }else{
                    self.invoice = response.data.invoice;
                    self.logo = response.data.invoice.logo;
                    self.pay_status_map = response.data.invoice.pay_status_map;
                    self.order = response.data.invoice.order;
                    self.orderDetails = response.data.invoice.details;
                }
            }).catch(function (error) {
                alert( error.message || error.statusText );
            })
        },

        /**
         *  获取订单id
         *  @return {number} id;
         */
        currentOrderId: function () {
            return parseInt(location.pathname.split('/').pop());
        },

        /**
         * 主商品id
         * @param id
         * @return {{}}
         */
        getMainProduct: function(id){
            var production = {};
            this.orderDetails.forEach(function (itme) {
                if (itme.id === id){
                    production = itme;
                }
            });
            return production;
        },

        /**
         * 获取商品对象
         * @return {object} product 商品详情对象
         */
        getProduct: function (extra) {
            return extra.product? extra.product : {};
        },

        /**
         * 判断是否为null
         * @param val
         * @return {boolean}
         */
        isNull: function (val) {
            return typeof val === 'object' && !val;
        },

        /**
         * 返回验光数据
         * @param {object} optometry_data 验光数据对象
         * @return {string} html内容
         */
        getOptometryData: function (optometry_data) {
            var html_str = '';
            for (var key in optometry_data){
                html_str +=  key + ':' + optometry_data[key] + '<br>';
            }
            return html_str;
        }
    },

    mounted: function () {
        var order_id = this.currentOrderId();
        this.fetchOrderDetail(order_id);
    }
});
 
